package com.demo.controller;

import com.demo.common.Result;
import com.demo.entity.TestObject;
import com.demo.service.TestService;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private TestService testService;

    @GetMapping("/selectAll")
    public Result selectAll() {
        List<TestObject> list = testService.selectAll();
        return Result.success(list);
    }

    /**
     * 路径传参
     */
    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        List<TestObject> list = testService.selectById(id);
        return Result.success(list);
    }

    /**
     * url传参
     */
    @GetMapping("/selectOne")
    public Result selectOne(@RequestParam Integer id) {
        List<TestObject> list = testService.selectById(id);
        return Result.success(list);
    }

    /**
     * 分页查询
     * pageNum:当前页
     * pageSize:每页数量
     */
    @GetMapping("/selectPage")
    public Result selectPage(@RequestParam(defaultValue = "1") Integer pageNum,
                             @RequestParam(defaultValue = "5") Integer pageSize) {
        PageInfo<TestObject> pageInfo = testService.selectPage(pageNum, pageSize);
        return Result.success(pageInfo);
    }

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public Result add(@RequestBody TestObject testObject)
    {
        testService.add(testObject);
        return Result.success();
    }

    /**
     * 更新数据
     */
    @PutMapping("/update")
    public Result update(@RequestBody TestObject testObject)
    {
        testService.update(testObject);
        return Result.success();
    }

    /**
     * 删除单个数据
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id)
    {
        testService.deleteById(id);
        return Result.success();
    }
}
